package com.zhongwang.mms.module.buy.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.BReportPlan;
import com.zhongwang.mms.gen.entity.BShoppingPlan;
import com.zhongwang.mms.gen.entity.POrderDetail;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.buy.service.OutsourceReturnService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**********************************************
 * @title: OutsourceReturnController
 * @description: 外协件确认回货控制层
 * @author: 徐一贺
 * @date: 2020/11/3 18:11
 * @versron: ver 1.0
 **********************************************
 */
@Controller
@RequestMapping("/buy/outsourceReturn")
public class OutsourceReturnController extends BaseController {

    @Autowired
    private OutsourceReturnService service;

    /**
     * @title: page
     * @description: 跳转到外协件确认回货
     * @param: [model]
     * @return: java.lang.String
     * @author: 徐一贺
     * @date: 2020/11/3 18:15
     * @versron: ver 1.0
     */
    @GetMapping
    public String page(Model model) {
        return "buy/outsourceReturn/page";
    }

    /**
     * 外协件回货列表数据
     *
     * @param shoppingPlanModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"plan:outsourceretconfirm:view"})
    public JsonResult list(ShoppingPlanModel shoppingPlanModel) {
        startPage();
        List<ShoppingPlanModel> list = service.findOutsourceReturnList(shoppingPlanModel);
        return jsonResult(list);
    }

    /**
     * 确认回货
     *
     * @param sppId 采购计划主键
     * @param odaId 入库主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/submit")
    @RequiresPermissions({"plan:outsourceretconfirm:confirm"})
    public JsonResult submit(Integer sppId, Integer odaId) {
        return jsonResult(service.submitReturn(sppId, odaId));
    }

    /**
     * 检测 项目号 SAP物料编码 跟踪号 图号 数量
     *
     * @param sppId 采购计划主键
     * @param odaId 入库主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/checkProCodeAndTrackNo")
    @RequiresPermissions({"plan:outsourceretconfirm:confirm"})
    public JsonResult checkProCodeAndTrackNo(Integer sppId, Integer odaId) {
        if (service.checkProCodeAndTrackNo(sppId, odaId) > 0) {
            return success("您确认的入库单的项目号或SAP物料编码或跟踪号或图号或数量不同，您确认要执行回货确认码？");
        } else {
            return error();
        }
    }

    /**
     * 批量确认回货
     *
     * @param sppId 采购计划主键
     * @param odaId 入库单主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/submit/all")
    @RequiresPermissions({"plan:outsourceretconfirm:confirm"})
    public JsonResult submit(Integer[] sppId, Integer[] odaId) {
        return jsonResult(service.submitReturn(sppId, odaId));
    }

    /**
     * 撤消回货确认
     *
     * @param id 主键
     * @return json
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"plan:outsourceretconfirm:confirm"})
    public JsonResult cancel(Integer id) {
        return jsonResult(service.cancelRetrun(id));
    }

    /**
     * 查询所有入库记录
     *
     * @param shoppingPlanModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/store/list")
    @RequiresPermissions({"plan:outsourceretconfirm:view"})
    public JsonResult storeList(ShoppingPlanModel shoppingPlanModel) {
        startPage();
        List<ShoppingPlanModel> list = service.findOrderDetail(shoppingPlanModel);
        return jsonResult(list);
    }
}
